﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BCCommon;
using BCCommon.FMISEnums;
using BCCommon.OAEnums;
using BCCommon.SCMEnums;
using BCDto.OA.TeamWorks.TaskRecord;
using BCDto.OA.TeamWorks.TaskRecordCount;
using BCDto.OA.TeamWorks.TaskRejectRecord;
using BCService.OA.TeamWorks.FMISTaskRecordService;
using Microsoft.AspNetCore.Mvc;

namespace BlackCoffeeRESTful.Controllers.Management.OA.Workflow.TaskRecord
{
    /// <summary>
    /// 财务部任务记录
    /// </summary>
    [ApiExplorerSettings(GroupName = "OAAdmin")]
    [Route("api/v1/sim/[controller]")]
    public class FMISTaskRecordController : ManagementBaseController
    {
        private readonly IFMISTaskRecordService fMISTaskRecordService;

        public FMISTaskRecordController(IFMISTaskRecordService fMISTaskRecordService)
        {
            this.fMISTaskRecordService = fMISTaskRecordService;
        }

        #region 获取财务部任务数
        /// <summary>
        /// 获取财务部任务数
        /// </summary>
        /// <param name="adminId">管理员Id</param>
        /// <response code="200">带有分页信息的日报列表。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(FMISTaskRecordCountDto), 200)]
        public IActionResult Counts(
            [FromQuery(Name = "admin_id")]long? adminId)
        {
            if (this.Admin.IsSystemAccount)
            {
                if (adminId.HasValue)
                {
                    return this.Ok(this.fMISTaskRecordService.TodayRecordCount(adminId.Value));
                }
                return this.Ok(this.fMISTaskRecordService.TodayRecordCount(null));
            }
            return this.Ok(this.fMISTaskRecordService.TodayRecordCount(this.Admin.AdminId));
        }
        #endregion

        #region 获取财务部任务记录（分页）
        /// <summary>
        /// 获取财务部任务记录（分页）
        /// </summary>
        /// <param name="adminId">管理员Id</param>
        /// <param name="handleStatus">处理状态</param>
        /// <param name="taskType">类型</param>
        /// <param name="isException">是否异常</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="date">日期</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="ascending">排序</param>
        /// <param name="taskExecuterFullName">制单人</param>
        /// <param name="saleContractNo">销售合同编号</param>
        /// <param name="purchaseContractNo">采购合同编号</param>
        /// <param name="buyerName">购买方</param>
        /// <param name="vehicleBrandName">品牌</param>
        /// <param name="pageIndex">以 1 起始的页码 (默认 1)。</param>
        /// <param name="pageSize">每页要显示的行数 (默认 10)。</param>
        /// <response code="200">带有分页信息的日报列表。</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(PageableList<FMISTaskRecordDto>), 200)]
        public IActionResult Get([FromQuery(Name = "admin_id")]long? adminId,
            [FromQuery(Name = "date")]DateTime? date,
            [FromQuery(Name = "start_date")]DateTime? startDate,
            [FromQuery(Name = "end_date")]DateTime? endDate,
            [FromQuery(Name = "handle_status")]OATaskHandleStatus? handleStatus,
            [FromQuery(Name = "task_type")]FMISTaskType? taskType,
            [FromQuery(Name = "is_exception")]bool? isException,
            [FromQuery(Name = "sort_name")]string sortName,
            [FromQuery(Name = "ascending")]bool? ascending,
            [FromQuery(Name = "task_executer_full_name")]string taskExecuterFullName,
            [FromQuery(Name = "sale_contract_no")]string saleContractNo,
            [FromQuery(Name = "purchase_contract_no")]string purchaseContractNo,
            [FromQuery(Name = "vehicle_brand_name")]string vehicleBrandName,
            [FromQuery(Name = "buyer_name")]string buyerName,
            int pageIndex = 1,
            int pageSize = 10)
        {
            long? executerAdminId = this.Admin.AdminId;
            int? tempCompanyId = this.Admin.CompanyId;
            if (this.Admin.IsSystemAccount)
            {
                executerAdminId = null;
                tempCompanyId = null;
            }
            var result = this.fMISTaskRecordService.Query(executerAdminId, date, startDate, endDate, handleStatus,
                taskType, isException, sortName, ascending, taskExecuterFullName, pageIndex, pageSize, tempCompanyId,
                saleContractNo, purchaseContractNo, vehicleBrandName, buyerName);
            return this.Ok(result);
        }
        #endregion

        #region 获取财务部活动实例驳回记录列表
        /// <summary>
        /// 获取财务部活动实例驳回记录列表
        /// </summary>
        /// <param name="workflowActivityInstanceId">工作流活动实例Id</param>
        /// <response code="200">获取财务部活动实例驳回记录列表。</response>
        /// <returns></returns>
        [HttpGet("[action]/{workflow_activity_instance_id}")]
        [ProducesResponseType(typeof(IEnumerable<TaskRejectRecordDto>), 200)]
        public IActionResult RejectRecords([FromRoute(Name = "workflow_activity_instance_id")]long workflowActivityInstanceId)
        {
            if (this.Admin.IsSystemAccount)
            {
                return this.Ok(this.fMISTaskRecordService.RejectRecords(null, workflowActivityInstanceId));
            }
            return this.Ok(this.fMISTaskRecordService.RejectRecords(this.Admin, workflowActivityInstanceId));
        }
        #endregion
    }
}